<template>
  <div class="apply">
    <el-form ref="applyForm" :model="applyForm" label-width="120px">
      <div class="nav-title">选择学员</div>
      <el-form-item v-if="!studentInfo" label="学员">
        <el-select v-model="applyForm.student" placeholder="请选择" @change="selectStudentClick">
          <el-option
            v-for="item in studentOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="text" @click="newStudentDialog= true">新增学员</el-button>
      </el-form-item>
      <div v-if="studentInfo" class="student-show">
        <div class="list-item">
          <span class="label">姓名：</span>
          <span>{{ studentInfo.name }}</span>
        </div>
        <div class="list-item">
          <span class="label">性别：</span>
          <span>{{ studentInfo.sex=='man'?'男':'女' }}</span>
        </div>
        <div class="list-item">
          <span class="label">手机号：</span>
          <span>{{ studentInfo.contact }}</span>
        </div>
        <div class="list-item">
          <span class="label">已报课程：</span>
          <span>{{ studentInfo.applyCourse }}</span>
        </div>
        <div style="margin-left:auto;">
          <el-button type="text" @click="newStudentDialog=true">编辑</el-button>
          <el-button type="text" @click="studentInfo=''">清除</el-button>
        </div>
      </div>
      <div class="nav-title">选择报读课程</div>
      <el-form-item label="课程">
        <el-button v-if="!applyForm.course" @click="selectCourseDialog=true">选择课程</el-button>
        <div v-if="applyForm.course" class="course-tag">
          <div class="course-title">{{ applyForm.course }}</div>
          <el-button v-if="applyForm.course" @click="selectCourseDialog=true">更换课程</el-button>
        </div>
      </el-form-item>
      <div class="nav-title">经办信息</div>
      <el-form-item>
        <div class="list-box">
          <div class="list-item">
            <span class="list-label">经办日期</span>
            <el-date-picker v-model="applyForm.time" type="date" placeholder="选择日期" />
          </div>
          <div class="list-item">
            <span class="list-label">经办人</span>
            <el-input v-model="applyForm.operator" placeholder="请输入经办人手机号" style="width:220px;" />
          </div>
        </div>
      </el-form-item>
    </el-form>
    <!-- 编辑弹框 -->
    <el-dialog :visible.sync="newStudentDialog" title="新增学员" width="650px">
      <el-form
        ref="newStudentForm"
        :model="newStudentForm"
        :rules="newStudentRules"
        label-width="150px"
      >
        <el-form-item label="学员姓名" prop="name">
          <el-input v-model="newStudentForm.name" style="width:300px;" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="newStudentForm.sex">
            <el-radio label="man">男</el-radio>
            <el-radio label="woman">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="contact">
          <el-input v-model="newStudentForm.contact" style="width:300px;" />
        </el-form-item>
        <el-form-item label="出身日期" prop="birthday">
          <el-date-picker
            v-model="newStudentForm.birthday"
            type="date"
            placeholder="选择日期"
            style="width:300px;"
          />
        </el-form-item>
        <el-form-item label="微信号">
          <el-input v-model="newStudentForm.wechat" style="width:300px;" />
        </el-form-item>
        <el-form-item label="年级">
          <el-select v-model="newStudentForm.grade" placeholder="请选择" style="width:300px;">
            <el-option
              v-for="item in gradeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地域">
          <el-cascader
            v-model="newStudentForm.territory"
            :options="territoryOptions"
            style="width:300px;"
          />
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="newStudentForm.address" style="width:300px;" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="newStudentCancel">取 消</el-button>
        <el-button type="primary" @click="newStudentConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 选择课程弹框 -->
    <createCourseDialog
      :dialog="selectCourseDialog"
      @cancelClick="cancelClick"
      @confirmClick="confirmCourseClick"
    />
  </div>
</template>

<script>
import createCourseDialog from '../class/coursedialog'

export default {
  components: {
    createCourseDialog
  },
  data() {
    return {
      studentInfo: '',
      applyForm: {
        student: '',
        courseId: '',
        course: '',
        time: '',
        operator: ''
      },
      studentOptions: [
        {
          value: '刘德华',
          label: '刘德华'
        }
      ],
      selectCourseDialog: false,
      newStudentDialog: false,
      // 编辑弹框绑定表单数据
      newStudentForm: {
        name: '',
        birthday: '',
        sex: 'man',
        age: '',
        wechat: '',
        territory: [],
        address: '',
        grade: '',
        contact: '',
        studentNum: 'S2019080515'
      },
      territoryOptions: [
        {
          value: '北京',
          label: '北京',
          children: [{
            value: '市辖区',
            label: '市辖区',
            children: [{
              value: '海淀区',
              label: '海淀区'
            }, {
              value: '朝阳区',
              label: '朝阳区'
            }, {
              value: '东城区',
              label: '东城区'
            }, {
              value: '西城区',
              label: '西城区'
            }]
          }
          ]
        }
      ],
      gradeOptions: [
        {
          value: '一年级',
          label: '一年级'
        },
        {
          value: '二年级',
          label: '二年级'
        },
        {
          value: '三年级',
          label: '三年级'
        },
        {
          value: '四年级',
          label: '四年级'
        },
        {
          value: '五年级',
          label: '五年级'
        },
        {
          value: '六年级',
          label: '六年级'
        },
      ],
      newStudentRules: {
        name: [{ required: true, message: '请输入学员姓名', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      },
    }
  },
  methods: {
    selectStudentClick(val) {
      // 请求数据
      this.studentInfo = {
        name: '刘德华',
        sex: 'man',
        contact: '13531313131',
        applyCourse: '2'
      }
    },
    cancelClick() {
      this.selectCourseDialog = false
    },
    // 接收选择课程弹框事件
    confirmCourseClick(data) {
      this.selectCourseDialog = false
      this.applyForm.courseId = data.id
      this.applyForm.course = data.name
    },
    newStudentCancel() {
      this.newStudentDialog = false
      this.$refs.newStudentForm.resetFields()
    },
    newStudentConfirm() {
      this.$refs.newStudentForm.validate((valid) => {
        if (valid) {
          this.$refs.newStudentForm.resetFields()
          this.newStudentDialog = false
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.apply {
  .nav-title {
    padding-left: 10px;
    margin: 20px 0;
    font-size: 16px;
    font-weight: 600;
    color: #353535;
    border-left: 2px solid #2377e6;
  }
  .course-tag {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 480px;
    height: 60px;
    padding: 10px 20px 10px 10px;
    color: #353535;
    font-size: 0;
    background: #f5f7fa;
    .course-title {
      width: 210px;
      overflow: hidden;
      font-weight: 700;
      font-size: 14px;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
  .list-box {
    display: flex;
    margin-left: -104px;
    .list-item {
      margin-right: 40px;
      .list-label {
        display: inline-block;
        width: 100px;
        color: #606266;
        text-align: right;
        font-weight: 600;
        padding-right: 10px;
      }
    }
  }
  .student-show {
    background-color: #fafafa;
    padding: 10px;
    display: flex;
    align-items: center;
    .list-item {
      margin-right: 40px;
      font-size: 14px;
      color: #353535;
      .label {
        color: #606266;
        margin-right: 10px;
      }
    }
  }
}
</style>
